using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
#if LIVE
using Microsoft.Xna.Framework.GamerServices;
#endif
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Neat;

namespace Sectors
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class SectorsGame : NeatGame
    {
        public SectorsScreen SectorsScreen;
        public World World;
        string InitScriptPath, FirstTimeScriptPath;

        public SectorsGame(string[] args)
        {
            Setup();
            Screens["mainmenu"] = SectorsScreen = new SectorsScreen(this);
            Window.Title = "Sectors";
            if (args.Length > 0)
                FirstTimeScriptPath = args[0];
        }
        public SectorsGame(World w)
        {
            //designer preview mode
            World = w;
            Screens["mainmenu"] = SectorsScreen = new SectorsScreen(this, World);
            Window.AllowUserResizing = true;
            Window.Title = "Sectors - Designer Mode";
        }

        void Setup()
        {
            GameWidth = 800;
            GameHeight = 600;
            GameBackgroundColor = Color.MediumOrchid;
            FullScreen = false;
        }
        protected override void Initialize()
        {
            base.Initialize();
            HasConsole = true;
            ConsoleKey = Keys.OemTilde;
            AutoDraw = true;
            Graphics.ApplyChanges();
            Window.Title = "Sectors";
            if (!string.IsNullOrEmpty(InitScriptPath))
                Console.LoadBatch(InitScriptPath);
            //Disable2DRendering = true;
        }

        public override void FirstTime()
        {
            if (!string.IsNullOrEmpty(FirstTimeScriptPath))
                Console.LoadBatch(FirstTimeScriptPath);
            else if (World == null)
                Console.Run("call sector.txt");
            
            base.FirstTime();
        }

        protected override void Update(GameTime gameTime)
        {
            //GraphicsDevice.BlendState = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            base.Update(gameTime);
        }

        protected override void Behave(GameTime gameTime)
        {
            if (IsTapped(Keys.F11))
            {
                if (Graphics.IsFullScreen)
                    Graphics.ToggleFullScreen();
                else
                {
                    OutputResolution.X = GraphicsDevice.Adapter.CurrentDisplayMode.Width;
                    OutputResolution.Y = GraphicsDevice.Adapter.CurrentDisplayMode.Height;
                    Graphics.ToggleFullScreen();
                }
            }
            if (IsTapped(Keys.Pause))
                IsPaused = !IsPaused;
            base.Behave(gameTime);
        }
    }
}
